package com.snail.billing.page;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.android.net.HttpResult;
import com.android.net.HttpSession;
import com.android.net.OnHttpResultListener;
import com.android.util.AlertUtil;
import com.android.util.ResUtil;
import com.snail.billing.BillingCallback;
import com.snail.billing.DataCache;
import com.snail.billing.Resource;
import com.snail.billing.data.Account;
import com.snail.billing.data.AccountManager;
import com.snail.billing.net.BillingEncode;
import com.snail.billing.net.BillingHttpApp;
import com.snail.billing.os.DialogPage;
import com.snail.billing.session.GeneralLoginSession;
import com.snail.billing.session.ModifyPwdSession;
import com.snail.billing.util.AccountCaptcha;

public class ModifyPwdPage extends DialogPage implements View.OnClickListener, OnHttpResultListener {

	private TextView textTitle;
	private View buttonBack;

	private TextView textAccount;
	private EditText inputOldPwd;
	private EditText inputNewPwd1;
	private EditText inputNewPwd2;
	private View button;

	private Account currentAccount;
	private ModifyPwdSession modifyPwdSession;

	private String oldPwd;
	private String pwdString;

	@Override
	public int createLayoutId() {
		return ResUtil.getLayoutId(Resource.layout.snailbilling_modify_pwd_activity);
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		textTitle = (TextView) findViewById(ResUtil.getViewId(Resource.id.snailbilling_title_text));
		textTitle.setText(ResUtil.getString(Resource.string.snailbilling_modify_pwd_title));
		buttonBack = findViewById(ResUtil.getViewId(Resource.id.snailbilling_title_button_back));
		buttonBack.setOnClickListener(this);
		//
		textAccount = (TextView) findViewById(ResUtil.getViewId(Resource.id.snailbilling_modify_pwd_text_account));
		inputOldPwd = (EditText) findViewById(ResUtil.getViewId(Resource.id.snailbilling_modify_pwd_input_old_pwd));
		inputNewPwd1 = (EditText) findViewById(ResUtil.getViewId(Resource.id.snailbilling_modify_pwd_input_new_pwd1));
		inputNewPwd2 = (EditText) findViewById(ResUtil.getViewId(Resource.id.snailbilling_modify_pwd_input_new_pwd2));
		button = findViewById(ResUtil.getViewId(Resource.id.snailbilling_modify_pwd_button));
		button.setOnClickListener(this);
		//
		currentAccount = AccountManager.getCurrentAccount();
		textAccount.setText(currentAccount.getAccount());
		if (currentAccount.getType().equals(Account.TYPE_RANDOM_OLD)) {
			inputOldPwd.setText(currentAccount.getPwd());
		}
	}

	@Override
	public void onClick(View v) {
		if (v.equals(buttonBack)) {
			getPageManager().backward();
		} else if (v.equals(button)) {
			oldPwd = inputOldPwd.getText().toString();
			if (TextUtils.isEmpty(oldPwd)) {
				AlertUtil.show(getContext(), ResUtil.getString(Resource.string.snailbilling_modify_pwd_input_old_pwd));
				return;
			}
			pwdString = inputNewPwd1.getText().toString();
			if (TextUtils.isEmpty(pwdString)) {
				AlertUtil.show(getContext(), ResUtil.getString(Resource.string.snailbilling_modify_pwd_input_new_pwd1));
				return;
			}
			String pwdString2 = inputNewPwd2.getText().toString();
			if (TextUtils.isEmpty(pwdString2)) {
				AlertUtil.show(getContext(), ResUtil.getString(Resource.string.snailbilling_modify_pwd_input_new_pwd2));
				return;
			}
			if (!AccountCaptcha.validatePwd(getContext(), currentAccount.getAccount(), pwdString, pwdString2)) {
				return;
			}
			if (pwdString.equals(oldPwd)) {
				AlertUtil.show(getContext(), ResUtil.getString(Resource.string.snailbilling_pwd_validate6));
				return;
			}
			requestModifyPwd();
		}
	}

	private void requestModifyPwd() {
		BillingHttpApp billingHttpApp = new BillingHttpApp(getContext(), new BillingHttpApp.RequestHttpSesssionListener() {

			@Override
			public HttpSession getLoginHttpSession() {
				return new GeneralLoginSession(currentAccount.getAccount(), currentAccount.getPwd());
			}

			@Override
			public HttpSession getRequestHttpSesssion() {
				modifyPwdSession = new ModifyPwdSession(oldPwd, pwdString);
				return modifyPwdSession;
			}
		});
		billingHttpApp.setOnHttpResultListener(this);
		billingHttpApp.request();
	}

	@Override
	public void onHttpResult(HttpResult httpResult) {
		if (httpResult.isSucceed()) {
			HttpSession httpSession = httpResult.getHttpSession();
			if (httpSession.equals(modifyPwdSession)) {
				ModifyPwdSession.JsonData jsonData = new ModifyPwdSession.JsonData((String) httpSession.getResponseData());
				if (jsonData.getCode() == 1) {
					currentAccount.setPwd(pwdString);
					AccountManager.setAccount(currentAccount);
					getPageManager().backward();
					if (DataCache.getInstance().callbackIsMd5) {
						String action = BillingCallback.ACTION_MODIFY_PWD_MD5;
						DataCache.getInstance().importParams.billingCallback.onCallback(1, action, new String[] { BillingEncode.MD5(pwdString) });
					} else {
						String action = BillingCallback.ACTION_MODIFY_PWD;
						DataCache.getInstance().importParams.billingCallback.onCallback(1, action, new String[] { pwdString });
					}
					Toast.makeText(getContext(), ResUtil.getString(Resource.string.snailbilling_modify_pwd_ok), Toast.LENGTH_SHORT).show();
				} else {
					if (!TextUtils.isEmpty(jsonData.getErrorMsg())) {
						Toast.makeText(getContext(), jsonData.getErrorMsg(), Toast.LENGTH_SHORT).show();
					} else {
						Toast.makeText(getContext(), jsonData.getMessage(), Toast.LENGTH_SHORT).show();
					}
				}
			}
		}
	}

}
